package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.limao.DBDataClass.CrowdPrestigeGoodsDataClass;
import com.limao.DBProvider.CrowdPrestigeGoodsProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class CrowdPrestigeGoodsCache  extends BasicModule{
	
	private CrowdPrestigeGoodsProvider provider;
	private String key="all";
	private String key_vr="all_vr";
	
	private static CrowdPrestigeGoodsCache instance;

    public static CrowdPrestigeGoodsCache getInstance() {
        return instance;
    }
 
    private Cache<String, String> prestigeGoodsCache;

    public CrowdPrestigeGoodsCache() {
    	super("CrowdPrestigeGoodsCache");
    	prestigeGoodsCache = CacheFactory.createCache("sys_crows_PrestigeGoodsCache");
    	
    	initProvider();
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    

	
    public void ClearObjectCache()
    {
    	prestigeGoodsCache.clear();
    }  
    
    public List<CrowdPrestigeGoodsDataClass> Load_PrestigeGoods_All(int userid)
    {
     	List<CrowdPrestigeGoodsDataClass> list=new ArrayList<CrowdPrestigeGoodsDataClass>();
    	String jsonStr=prestigeGoodsCache.get(key);
    	if(CacheClass.isEmpty(jsonStr))
    	{
    		list=provider.PrestigeGoods();
    		if(list!=null && list.size()>0)
    		{
    			jsonStr=JSON.toJSONString(list);
    			prestigeGoodsCache.put(key, jsonStr);
    		}
    	}
    	else
    	{
    		CrowdPrestigeGoodsDataClass[] nodes = JSON.parseObject(jsonStr, new TypeReference<CrowdPrestigeGoodsDataClass[]>(){});    
	        list.addAll(Arrays.asList(nodes));
    	}
    	
    	//
    	if(list!=null && list.size()>0)
    		list=provider.PrestigeGoodsByuserid(userid,list);
    	
    	
     	return list;
    }
    
    
    
    
    public CrowdPrestigeGoodsDataClass Load_PrestigeGoods_ById(int prestigeid)
    {
     	List<CrowdPrestigeGoodsDataClass> list=new ArrayList<CrowdPrestigeGoodsDataClass>();
    	String jsonStr=prestigeGoodsCache.get(key);
    	if(CacheClass.isEmpty(jsonStr))
    	{
    		list=provider.PrestigeGoods();
    		if(list!=null && list.size()>0)
    		{
    			jsonStr=JSON.toJSONString(list);
    			prestigeGoodsCache.put(key, jsonStr);
    		}
    	}
    	else
    	{
    		CrowdPrestigeGoodsDataClass[] nodes = JSON.parseObject(jsonStr, new TypeReference<CrowdPrestigeGoodsDataClass[]>(){});    
	        list.addAll(Arrays.asList(nodes));
    	}
    	
    	CrowdPrestigeGoodsDataClass tmp=null;
    	//
    	if(list!=null && list.size()>0)
    	for(CrowdPrestigeGoodsDataClass obj:list)
        {
        	if(CacheClass.getIntValue(obj.prestigeid)==prestigeid)
        	{
        		tmp=obj;
        		break;
        	}
        		
        }
     	return tmp;
    }
    
    public CrowdPrestigeGoodsDataClass Load_PrestigeGoods_ById(int userid,int prestigeid)
    {
     	List<CrowdPrestigeGoodsDataClass> list=new ArrayList<CrowdPrestigeGoodsDataClass>();
    	String jsonStr=prestigeGoodsCache.get(key);
    	if(CacheClass.isEmpty(jsonStr))
    	{
    		list=provider.PrestigeGoods();
    		if(list!=null && list.size()>0)
    		{
    			jsonStr=JSON.toJSONString(list);
    			prestigeGoodsCache.put(key, jsonStr);
    		}
    	}
    	else
    	{
    		CrowdPrestigeGoodsDataClass[] nodes = JSON.parseObject(jsonStr, new TypeReference<CrowdPrestigeGoodsDataClass[]>(){});    
	        list.addAll(Arrays.asList(nodes));
    	}
    	
    	if(list!=null && list.size()>0)
    		list=provider.PrestigeGoodsByuserid(userid,list);
    	
    	CrowdPrestigeGoodsDataClass tmp=null;
    	//
    	if(list!=null && list.size()>0)
    	for(CrowdPrestigeGoodsDataClass obj:list)
        {
        	if(CacheClass.getIntValue(obj.prestigeid)==prestigeid)
        	{
        		tmp=obj;
        		break;
        	}
        		
        }
     	return tmp;
    }
    
    //虚拟币商城商品列表
    public List<CrowdPrestigeGoodsDataClass> Load_PrestigeGoods_Vr_All(int userid)
    {
     	List<CrowdPrestigeGoodsDataClass> list=new ArrayList<CrowdPrestigeGoodsDataClass>();
    	String jsonStr=prestigeGoodsCache.get(key_vr);
    	if(CacheClass.isEmpty(jsonStr))
    	{
    		list=provider.PrestigeVrGoods();
    		if(list!=null && list.size()>0)
    		{
    			jsonStr=JSON.toJSONString(list);
    			prestigeGoodsCache.put(key_vr, jsonStr);
    		}
    	}
    	else
    	{
    		CrowdPrestigeGoodsDataClass[] nodes = JSON.parseObject(jsonStr, new TypeReference<CrowdPrestigeGoodsDataClass[]>(){});    
	        list.addAll(Arrays.asList(nodes));
    	}
    	
   	
     	return list;
    }
   
    public CrowdPrestigeGoodsDataClass Load_PrestigeGoods_Vr_Id(int userid,int prestigeid)
    {
     	List<CrowdPrestigeGoodsDataClass> list=new ArrayList<CrowdPrestigeGoodsDataClass>();
    	String jsonStr=prestigeGoodsCache.get(key_vr);
    	if(CacheClass.isEmpty(jsonStr))
    	{
    		list=provider.PrestigeVrGoods();
    		if(list!=null && list.size()>0)
    		{
    			jsonStr=JSON.toJSONString(list);
    			prestigeGoodsCache.put(key_vr, jsonStr);
    		}
    	}
    	else
    	{
    		CrowdPrestigeGoodsDataClass[] nodes = JSON.parseObject(jsonStr, new TypeReference<CrowdPrestigeGoodsDataClass[]>(){});    
	        list.addAll(Arrays.asList(nodes));
    	}
    	
    	CrowdPrestigeGoodsDataClass tmp=null;
    	if(list!=null)
    	{
	    	for(CrowdPrestigeGoodsDataClass obj:list)
	    	{
	    		if(prestigeid==CacheClass.getIntValue(obj.prestigeid))
	    		{
	    			tmp=obj;
	    			break;
	    		}
	    	}
    	}
    	
     	return tmp;
    }
    
    public CrowdPrestigeGoodsDataClass Load_PrestigeVrGoods_ById(int prestigeid)
    {
     	List<CrowdPrestigeGoodsDataClass> list=new ArrayList<CrowdPrestigeGoodsDataClass>();
    	String jsonStr=prestigeGoodsCache.get(key_vr);
    	if(CacheClass.isEmpty(jsonStr))
    	{
    		list=provider.PrestigeVrGoods();
    		if(list!=null && list.size()>0)
    		{
    			jsonStr=JSON.toJSONString(list);
    			prestigeGoodsCache.put(key_vr, jsonStr);
    		}
    	}
    	else
    	{
    		CrowdPrestigeGoodsDataClass[] nodes = JSON.parseObject(jsonStr, new TypeReference<CrowdPrestigeGoodsDataClass[]>(){});    
	        list.addAll(Arrays.asList(nodes));
    	}
    	
    	CrowdPrestigeGoodsDataClass tmp=null;
    	//
    	if(list!=null && list.size()>0)
    	for(CrowdPrestigeGoodsDataClass obj:list)
        {
        	if(CacheClass.getIntValue(obj.prestigeid)==prestigeid)
        	{
        		tmp=obj;
        		break;
        	}
        		
        }
     	return tmp;
    }
//******************************************************
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =CrowdPrestigeGoodsProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }
  
}

